﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Linq;

namespace QuanLyKTX
{
    using DAL;
    using Telerik.WinControls.UI;
    public partial class frm_ListContracts : Telerik.WinControls.UI.RadForm
    {
        RadContextMenu contextMenu;
        RadContextMenu tContextMenu;
        private void BindHopDong(bool daThanhLy)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                if (!daThanhLy)
                {
                    var results = from p in db.tbl_HopDongs
                                  join q in db.tbl_SinhViens on p.ID equals q.ID
                                  where p.NgayThanhLyHD == null
                                  select new
                                  {
                                      p.MaHopDong,
                                      p.ID,
                                      SoHD = string.Format("{0}/{1}", p.SoHopDong, p.HauTo),
                                      q.MaSinhVien,
                                      HoTen = String.Format("{0} {1}", q.Ho, q.Ten),
                                      p.NgayBatDauHD,
                                      p.NgayKetThucHD,
                                      p.NgayThanhLyHD
                                  };

                    radGridView_HopDong.DataSource = results;
                    radGridView_HopDong.AutoGenerateColumns = false;
                }
                else
                {
                    var results = from p in db.tbl_HopDongs
                                  join q in db.tbl_SinhViens on p.ID equals q.ID
                                  where p.NgayThanhLyHD != null
                                  select new
                                  {
                                      p.MaHopDong,
                                      p.ID,
                                      SoHD = string.Format("{0}/{1}", p.SoHopDong, p.HauTo),
                                      q.MaSinhVien,
                                      HoTen = String.Format("{0} {1}", q.Ho, q.Ten),
                                      p.NgayBatDauHD,
                                      p.NgayKetThucHD,
                                      p.NgayThanhLyHD
                                  };

                    radGridView_HDDaThanhLy.DataSource = results;
                    radGridView_HDDaThanhLy.AutoGenerateColumns = false;
                }

            }
        }

        #region Private Methods

        private void UpdHopDong(Guid maHopDong)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                // Cập nhật hợp đồng
                var result = (from p in db.tbl_HopDongs
                              where p.MaHopDong == maHopDong
                              select p).Single();

                result.NgayThanhLyHD = db.GetSystemDate().Single().Column1;

                int id = result.ID;

                // Cập nhật Xếp phòng
                var resultXepPhong = (from p in db.tbl_XepPhongs
                                      where p.ID == id && p.NgayRa == null
                                      select p).Single();
                if (resultXepPhong != null)
                {

                    db.tbl_XepPhongs.DeleteOnSubmit(resultXepPhong);

                    string soPhong = resultXepPhong.SoPhong;

                    // Cập nhật Giường trống
                    var resultPhong = (from p in db.tbl_Phongs
                                       where p.SoPhong == soPhong
                                       select p).Single();
                    int soGiuongTrong = Convert.ToInt32(resultPhong.SoGiuongTrong) + 1;
                    resultPhong.SoGiuongTrong = Convert.ToByte(soGiuongTrong);

                    // Xóa Hóa đơn phòng
                    var resultHoaDonPhong = from p in db.tbl_HoaDonPhongs
                                            where p.MaHopDong == maHopDong && p.DaThanhToan == false
                                            select p;
                    if (resultHoaDonPhong.Count() > 0)
                    {
                        db.tbl_HoaDonPhongs.DeleteAllOnSubmit(resultHoaDonPhong);
                    }
                }

                db.SubmitChanges();
            }
        }

        private void InitContextMenu()
        {
            contextMenu = new RadContextMenu();
            RadMenuItem menuItemThanhLy = new RadMenuItem("Thanh lý hợp đồng");
            menuItemThanhLy.Click += new EventHandler(menuItemThanhLy_Click);
            RadMenuItem menuItemIn = new RadMenuItem("In");
            menuItemIn.Click += new EventHandler(menuItemIn_Click);

            contextMenu.Items.Add(menuItemIn);
            contextMenu.Items.Add(menuItemThanhLy);

        }

        private void TInitContextMenu()
        {
            tContextMenu = new RadContextMenu();
            RadMenuItem menuTItemIn = new RadMenuItem("In");
            menuTItemIn.Click += new EventHandler(menuTItemIn_Click);

            tContextMenu.Items.Add(menuTItemIn);
        }
        #endregion

        #region Event

        private void radGridView_HDDaThanhLy_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            if (!radGridView_HDDaThanhLy.CurrentRow.IsSystem)
            {
                e.ContextMenu = tContextMenu.DropDown;
            }
        }

        private void radGridView_HopDong_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            if (!radGridView_HopDong.CurrentRow.IsSystem)
            {
                e.ContextMenu = contextMenu.DropDown;
            }
        }

        void menuItemThanhLy_Click(object sender, EventArgs e)
        {
            //UpdHopDong(new Guid(radGridView_HopDong.CurrentRow.Cells[0].Value.ToString()));
            using (var frmEndContrack = new frm_EndContract(new Guid(radGridView_HopDong.CurrentRow.Cells[0].Value.ToString())))
            {
                frmEndContrack.ShowDialog();
                BindHopDong(true);
                BindHopDong(false);
            }
        }

        private void frm_ListContracts_Load(object sender, EventArgs e)
        {
            BindHopDong(true);
            BindHopDong(false);
            InitContextMenu();
            TInitContextMenu();
        }

        public frm_ListContracts()
        {
            InitializeComponent();
        }

        void menuItemIn_Click(object sender, EventArgs e)
        {

            using (var db = new QuanLyKTXDataContext())
            {
                int id = Convert.ToInt32(radGridView_HopDong.CurrentRow.Cells[1].Value);
                string maHopDong = radGridView_HopDong.CurrentRow.Cells[0].Value.ToString();

                var sinhVien = (from p in db.tbl_SinhViens
                                where p.ID == id
                                select p).FirstOrDefault();

                var hopDong = (from p in db.tbl_HopDongs
                               where p.MaHopDong == new Guid(maHopDong)
                               select p).FirstOrDefault();


                using (frm_ReportContractViewer reportViewer = new frm_ReportContractViewer(sinhVien, hopDong))
                {
                    reportViewer.ShowDialog();
                }

            }
        }

        void menuTItemIn_Click(object sender, EventArgs e)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                int id = Convert.ToInt32(radGridView_HDDaThanhLy.CurrentRow.Cells[1].Value);
                string maHopDong = radGridView_HDDaThanhLy.CurrentRow.Cells[0].Value.ToString();

                var sinhVien = (from p in db.tbl_SinhViens
                                where p.ID == id
                                select p).FirstOrDefault();

                var hopDong = (from p in db.tbl_HopDongs
                               where p.MaHopDong == new Guid(maHopDong)
                               select p).FirstOrDefault();


                using (frm_ReportEndContact reportViewer = new frm_ReportEndContact(sinhVien, hopDong))
                {
                    reportViewer.ShowDialog();
                }

            }
        }

        #endregion
    }
}
